$("#targets").live('submit', function(e) {
    var is_empty = false;
    //var is_error = false;
    var invalid_phone = false;
    var invalid_email = false;
    var invalid_password = false;
    var invalid_captcha = false;
    var invalid_number = false;
    
    $("*[class^='mandatory']:not(span)").each(function() {
        var element_val = $(this).val();        
        var parent_element = $(this).parent().parent();
        if ((parent_element.css('display') != '') && (parent_element.css('display') != 'none')) {
            if(element_val == ''){
                is_empty = true;
                $(this).css('border-color', 'red');
            }
            else 
                $(this).css('border', '1px solid #DADADA');
        }        
    });    
    
    //Phone
    $("*[class$='phone']:not(span)").each(function() {    
        var element_val = $(this).val();  
        element_val = element_val.split(' ').join('');
        if(element_val != '') {
            if(element_val.length < 9) {
                invalid_phone = true;                
            }
            else {
                var reg_phone = /^[0-9]*$/;
                if(!reg_phone.test(element_val))
                    invalid_phone = true;
                //is_error = !reg_phone.test(element_val);
            }

            var next_element = $(this).parent();
            $(next_element).find('div.error').remove();
            if(invalid_phone) {
                $(next_element).append('<div class="error">Invalid phone number</div>');
            }
        }
    });
    
    //Email
    $("*[class$='email']:not(span)").each(function() {
        var element_val = $(this).val();
        element_val = element_val.split(' ').join('');
        if(element_val != '') {     
            var reg_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            if(!reg_email.test(element_val))
                invalid_email = true;
            //is_error = !reg_email.test(element_val);

            var next_element = $(this).parent();
            $(next_element).find('div.error').remove();
            if(invalid_email) {
                //alert('Email');
                $(next_element).append('<div class="error">Invalid email address</div>');
            }
        }
    });

    //Password
    $("*[class$='password']:not(span)").each(function() {            
        var element_val = $(this).val();
        if(element_val != '') {     
            if ((element_val.length < 6) || (element_val.length > 20)) {
                invalid_password = true;
            }
            else {                
                var reg_password = /^[a-zA-Z0-9]*$/;                
                if(!reg_password.test(element_val)) {
                    invalid_password = true;
                }
                //is_error = !reg_password.test(element_val);
            }

            var next_element = $(this).parent();
            $(next_element).find('div.error').remove();
            if(invalid_password) {
                $(next_element).append('<div class="error">Invalid password</div>');
            }
            else {
                var confirm_password = $("#confirm_password").val();
                if(element_val != confirm_password) {
                    invalid_password = true;
                    $(next_element).append('<div class="error">Mismatched password</div>');
                }
            }
        }
    });

    //Captcha
    $("*[class$='captcha']:not(span)").each(function() {        
        var element_val = $(this).val();
        if(element_val != '') {     
            var captcha_val = $("#captcha_word").val();
            if(element_val != captcha_val)
                invalid_captcha = true;

            var next_element = $(this).parent();
            $(next_element).find('div.error').remove();
            if(invalid_captcha) {
                $(next_element).append('<div class="error">Mismatched captcha</div>');
            }
        }
    });
    
    //Number
    $("*[class$='number']:not(span)").each(function() {    
        var element_val = $(this).val();        
        if(element_val != '') {            
            var reg_number = /^[0-9]*$/;
            if(!reg_number.test(element_val))
                invalid_number = true;

            var next_element = $(this).parent();
            $(next_element).find('div.error').remove();
            if(invalid_number) {
                $(next_element).append('<div class="error">Invalid number</div>');
            }
        }
    });
    
    if (is_empty || invalid_phone || invalid_email || invalid_password || invalid_captcha || invalid_number)
        e.preventDefault();
});

/*
$(document).ready(function(){
    $('#targets').submit(function() {        
        var error = 0;
        
        //Reset        
        //Member
        $('#membername_error_msg').html('').parent().hide();
        
        //Dealer
        $('#type_member_error_msg').html('').parent().hide();
        $('#company_error_msg').html('').parent().hide();
        $('#service_error_msg').html('').parent().hide();
        $('#company_phone_1_error_msg').html('').parent().hide();
        $('#company_phone_2_error_msg').html('').parent().hide();
        $('#website_error_msg').html('').parent().hide();
        $('#location_error_msg').html('').parent().hide();
        $('#address_error_msg').html('').parent().hide();
        $('#contact_phone_2_error_msg').html('').parent().hide();
        $('#position_error_msg').html('').parent().hide();
        $('#description_error_msg').html('').parent().hide();
        
        //Car
        $('#make_error_msg').html('').parent().hide();
        $('#model_error_msg').html('').parent().hide();
        $('#year_error_msg').html('').parent().hide();
        $('#type_error_msg_error_msg').html('').parent().hide();
        $('#category_error_msg').html('').parent().hide();
        $('#external_color_error_msg').html('').parent().hide();
        $('#price_error_msg').html('').parent().hide();
        $('#quality_error_msg').html('').parent().hide();
        $('#duration_error_msg').html('').parent().hide();
        
        //Generic
        $('#contact_phone_1_error_msg').html('').parent().hide();
        $('#contact_phone_2_error_msg').html('').parent().hide();        
        $('#password_error_msg').html('').parent().hide();
        $('#email_error_msg').html('').parent().hide();
        $('#login_error_msg').html('').parent().hide();
        $('#photo_error_msg').html('').parent().hide();
        $('#confirmCaptcha_error_msg').html('').parent().hide();

        //Validation
        //Member
        var membername =  $('#membername').val();
        if (membername != undefined)
        {
            if (membername == ''){
                error = 1;           
                $('#membername_error_msg').html("Name is required");
                $('#membername_error_msg').parent().show();
            }
        }

        //Dealer
        var type_member = $('#type_member').val();
        if (type_member != undefined)
        {
            if(type_member == ''){
                error = 1;
                $('#type_member_error_msg').html('Please select a type');
                $('#type_member_error_msg').parent().show();
            } 
        }

        var company = $('#company').val();
        if(company == ''){
            error = 1;           
            $('#company_error_msg').html("Phone is required");
            $('#company_error_msg').parent().show();
        }

        var company_phone = $('#company_phone_1').val();
        if (company_phone != undefined)
        {
            var reg_phone = /^[0-9]*$/;
            if(company_phone == ''){
                error = 1;
                $('#company_phone_1_error_msg').html('Phone is required');
                $('#company_phone_1_error_msg').parent().show();
            } 
            else if(company_phone.length < 9)
            {
                error = 1;
                $('#company_phone_1_error_msg').html('Invalid phone number');
                $('#company_phone_1_error_msg').parent().show();
            }
            else if(!reg_phone.test(company_phone)){
                error = 1;
                $('#company_phone_1_error_msg').html('Invalid phone number');
                $('#company_phone_1_error_msg').parent().show();
            } 
        }
        
        company_phone = $('#company_phone_2').val();
        if (company_phone != undefined)
        {
            var reg_phone = /^[0-9]*$/;
            if(!reg_phone.test(company_phone)){
                error = 1;
                $('#company_phone_2_error_msg').html('Invalid phone number');
                $('#company_phone_2_error_msg').parent().show();
            } 
        }        
        
        var email = $('#email').val();        
        var reg_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (email != undefined)
        {
            if (email == ''){
                error = 1;
                $('#email_error_msg').html('Email is required');
                $('#email_error_msg').parent().show();
            }  
            else if(!reg_email.test(email)){
                error = 1;
                $('#email_error_msg').html('Invalid email.');
                $('#email_error_msg').parent().show();
            } 
        }       

        var password = $('#password').val();
        var confirmpassword = $('#confirmpassword').val();
        if( password != confirmpassword){
            error = 1;
            $('#password_error_msg').html("Password mismatch");
            $('#password_error_msg').parent().show();
            $('#confirmpassword_error_msg').html("Password mismatch");          
            $('#confirmpassword_error_msg').parent().show();
        }
        else if (password == "" || confirmpassword == ""){
            error = 1;
            $('#password_error_msg').html("Password is required");
            $('#password_error_msg').parent().show();
            $('#confirmpassword_error_msg').html("Password is required");          
            $('#confirmpassword_error_msg').parent().show();  	        	
        }
        
        var contact_phone = $('#contact_phone_1').val();
        if (contact_phone != undefined)
        {
            var reg_phone = /^[0-9]*$/;
            if(contact_phone == ''){
                error = 1;
                $('#contact_phone_1_error_msg').html('Phone is required');
                $('#contact_phone_1_error_msg').parent().show();
            } 
            else if(contact_phone.length < 9)
            {
                error = 1;
                $('#contact_phone_1_error_msg').html('Invalid phone number');
                $('#contact_phone_1_error_msg').parent().show();                    
            }
            else if(!reg_phone.test(contact_phone)){
                error = 1;
                $('#contact_phone_1_error_msg').html('Invalid phone number');
                $('#contact_phone_1_error_msg').parent().show();
            } 
        }
        
        var contact_phone = $('#contact_phone_2').val();
        if (contact_phone != undefined)
        {
            var reg_phone = /^[0-9]*$/;
            if(!reg_phone.test(contact_phone)){
                error = 1;
                $('#contact_phone_2_error_msg').html('Invalid phone number');
                $('#contact_phone_2_error_msg').parent().show();
            } 
        }        
        
        //Car
        var make = $('#make').val();
        if (make != undefined)
        {
            if(make == ''){
                error = 1;
                $('#make_error_msg').html("Please select a make");
                $('#make_error_msg').parent().show();
            }
        }

        var model = $('#model').val();
        if (model != undefined)
        {
            if(model == ''){
                error = 1;
                $('#model_error_msg').html("Please select a model");
                $('#model_error_msg').parent().show();
            }
        }

        var year = $('#year').val();
        if (year != undefined)
        {
            if(year == ''){
                error = 1;
                $('#year_error_msg').html("Please select a year");
                $('#year_error_msg').parent().show();
            }
        }
        
        var car_type = $('#type').val();
        if (car_type != undefined)
        {
            if(car_type == ''){
                error = 1;
                $('#type_error_msg').html("Please select a type");
                $('#type_error_msg').parent().show();
            }
        }
       
        var category = $('#category').val();
        if (category != undefined)
        {
            if(category == ''){
                error = 1;
                $('#category_error_msg').html("Please select a category");
                $('#category_error_msg').parent().show();
            }
        }

        var external_color = $('#external_color').val();
        if (external_color != undefined)
        {
            if(external_color == ''){
                error = 1;
                $('#external_color_error_msg').html("Please select a color");
                $('#external_color_error_msg').parent().show();
            }
        }
        
        var price = $('#price').val();
        if (price != undefined)
        {      
            if(price != '')
            {
                if(isNaN(parseFloat(price))){
                    error = 1;
                    $('#price_error_msg').html("Invalid price");
                    $('#price_error_msg').parent().show();
                }
            }
        }
        
        var quality = $('#quality').val();
        if (quality != undefined)
        {   
            if(quality != '')
            {
                if(isNaN(parseInt(quality))){
                    error = 1;
                    $('#quality_error_msg').html("Invalid quality");
                    $('#quality_error_msg').parent().show();
                }
            }
        }
        
        var duration = $('#duration').val();        
        if (duration != undefined)
        {     
            if(duration != '')
            {
                if(isNaN(parseInt(duration))){
                    error = 1;
                    $('#duration_error_msg').html("Invalid duration");
                    $('#duration_error_msg').parent().show();
                }
            }
        }
                  
        if(error)
            return false;
        else
            return true;        
    });
});
*/